//
//  WSQTableView.m
//  WSQOCTools
//
//  Created by 翁胜琼 on 2019/1/17.
//  Copyright © 2019 Dev John. All rights reserved.
//

#import "WSQTableView.h"
#import "MJRefresh.h"
#import "UIScrollView+EmptyDataSet.h"
#import <Masonry/Masonry.h>

@interface WSQTableView()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>


@end

@implementation WSQTableView

- (instancetype)initWithViewModel:(__kindof WSQViewModel *)viewModel
{
    if (self = [super initWithViewModel:viewModel])
    {
        
    }
    return self;
}

- (instancetype)initWithframe:(CGRect)frame viewModel:(__kindof WSQViewModel *)viewModel
{
    if (self = [super initWithframe:frame viewModel:viewModel])
    {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
}

// MARK: - RACConfig
- (void)bindViewModel
{
    
}


// MARK: - setupUI
- (void)setupUI
{
    [super setupUI];
    [self setupTableView];
}

- (void)setupTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.showsVerticalScrollIndicator = false;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 隐藏 grouped 下顶部/底部间隙
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];

    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    
    [self addSubview:self.tableView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
}

// MARK: - DZNEmptyDataSet
// 指定空数据源时被点击的动作
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
    if (self.tableView.mj_header && self.tableView.mj_header.state != MJRefreshStateRefreshing) {
        [self.tableView.mj_header beginRefreshing];
    }
}

// 指定空数据源所展示的图片
//- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
//{
//    return [UIImage imageNamed:@"no-more Data"];
//}

// 调整视图内容偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return 0;
}

// 设置指定图片的tint 颜色
//- (UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView {
//    return [[UIColor yellowColor] colorWithAlphaComponent:0.3];
//}

//// 设置空数据源开启图片动画
//- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView {
//    return YES;
//}
//
////  设置图片动画
//- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
//
//    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
//
//    animation.duration = 0.25;
//    animation.cumulative = YES;
//    animation.repeatCount = MAXFLOAT;
//
//    return animation;
//}

//// 指定空数据源所需要展示的标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无更多数据，请刷新重试";
    UIColor *foregroundColor = [UIColor colorWithRed:51/255 green:51/255 blue:51/255 alpha:1];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName:foregroundColor
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

//// 指定空白数据源标题下的详细信息
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"此时暂无更多数据\n请刷新后重试";

    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    UIColor *foregroundColor = [UIColor colorWithRed:102/255 green:102/255 blue:102/255 alpha:1];

    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:12.0f],
                                 NSForegroundColorAttributeName:foregroundColor,
                                 NSParagraphStyleAttributeName:paragraph
                                 };

    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

// 设置空数据源显示按钮
//- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
//{
//    // 设置按钮标题
//    NSString *buttonTitle = @"请点击重试";
//    NSDictionary *attributes = @{
//                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f]
//                                 };
//    return [[NSAttributedString alloc] initWithString:buttonTitle attributes:attributes];
//}

// 返回自定义视图
//- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
//    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    [activityView startAnimating];
//    return activityView;
//}

// 设置空数据源时控件之间彼此间距
//- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
//    return 25.0f;
//}

// 设置数据源为空的时背景颜色
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
//    return [UIColor colorWithRed:249/255 green:249/255 blue:249/255 alpha:1];
    return [UIColor whiteColor];
}


// 空数据源视图即将出现
- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView
{
    self.tableView.mj_footer.hidden = YES;
}

// 空数据源视图即将消失
- (void)emptyDataSetWillDisappear:(UIScrollView *)scrollView
{
    self.tableView.mj_footer.hidden = NO;
}

// MARK: - DZNEmptyDataSetDelegate
// 空数据源按钮被点击
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    
}

// MARK: - MJRefresh
- (void)add_MJHeaderRefreshWithCompletionBlock:(void (^)(void))completionBlock
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:completionBlock];
}

- (void)add_MJFooterRefreshWithCompletionBlock:(void (^)(void))completionBlock
{
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:completionBlock];
}

- (void)end_MJRefresh
{
    // 主线程刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self end_MJHeaderRefresh];
        [self end_MjFooterRefresh];
        
        [self.tableView reloadData];
        
    });
}

- (void)end_MJHeaderRefresh
{
    // 主线程刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
}

- (void)end_MjFooterRefresh
{
    // 主线程刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.tableView.mj_footer endRefreshing];
    });
}

@end
